package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
@Api(value = "课程操作",tags = "接口路径地址-tags",description = "接口路径地址-des")
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;
    @Autowired
    private TeachplanService teachplanService;
    //机构提交课程
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    //查询所有课程
    @PostMapping("/course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseBaseModel model){
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        //2.调用service层的方法
        PageVO<CourseBaseDTO> pageVo = courseBaseService.queryCourseList(params, model, companyId);
        return pageVo;
    }
    //添加课程
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
        //设置公司和登录用户信息
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.createCourseBase(courseBaseDTO);
    }
    //获取回显数据
    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();

        CourseBaseDTO dto = courseBaseService.getByCourseId(courseBaseId, companyId);
        return dto;
    }
    //修改课程
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
        //设置公司和登录用户信息
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO courseBaseDTO1 = courseBaseService.modifyCourseBase(courseBaseDTO);

        return courseBaseDTO1;
    }
    //删除课程
    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }
}
